1 message in com.perforce.perforce-user[p4] "p4 protect" triggers?| From | Sent On | Attachments |
|---|---|---|
| jab | 24 Oct 2004 09:45 |
| Subject: | [p4] "p4 protect" triggers?![]() |
|---|---|
| From: | jab (ja...@pobox.com) |
| Date: | 10/24/2004 09:45:24 AM |
| List: | com.perforce.perforce-user |
I haven't seen that much traffic w.r.t. the new trigger mechanisms in 2004.2, and thought I'd through my entry into the race...
I've checked in an example trigger into my "scripts" subdir in the Public Depot. You'll find lots of examples of scripts written in Perl/P4Perl/Python/Ruby/P4Ruby, in //guest/jeff_bowles/scripts/... in the Public Depot.
The example trigger was a "just for grins" sort of example: "refuse to let anyone give permissions to 'user *', since it is a possible security hole."
It shows several things: 1) How to run a trigger that verifies a form; 2) You do not have to call 'p4' from inside the script itself, since all the information you want might already be in a file that you can just "open()"
Anyhow, if this is useful to you, let me know; if there are problems with using it, definitely let me know.
I figure, if you have a specific business need to make sure that certain things are never entered into "p4 protect" (such as giving herman permission to //depot/main/src/...), this is the way to go...
-jab
ps. If you are writing scripts, please consider checking examples into the Perforce Public Depot for others to use!
-------------------- //guest/jeff_bowles/scripts/trig_protect_paranoid.p4ruby#1 - add change 4603 (text) # Task: form trigger that refuses "p4 protect" entries that # give permissions to "user *" # # will work on 2004.2 (and later) Perforce servers, but you # need to remember to install the trigger thusly: # (p4 triggers line follows - the '#' is a comment char for Ruby.) # example in protect "ruby /Users/jeffbowles/work/ruby-work/contracts/tmp-trigger-scripts/ trig_protect_paranoid.p4ruby --formfile %formfile% --formname %formname%"
# # num of calls to 'p4': 0 # status: tested on Darwin Mac OS X using ruby (no P4API needed!) # # Copyright 2004 Piccolo Engineering, Inc. All rights reserved.
require "getoptlong"
options = GetoptLong.new( [ "--formfile", "--ff", GetoptLong::OPTIONAL_ARGUMENT], [ "--formname", "--fn", GetoptLong::OPTIONAL_ARGUMENT] ) defaultFormFile = nil defaultFormName = nil options.each do |opt, arg| case opt when "--formfile", "--ff" defaultFormFile = arg when "--formname", "--fn" defaultFormName = arg end end
raise Exception, "no --formfile given" if defaultFormFile.nil? raise Exception, "no --formname given" if defaultFormName.nil?
# fd = open("/Users/jeffbowles/work/ruby-work/contracts/tmp-trigger-scripts/ debug.out", "w") # fd.puts defaultFormFile unless defaultFormFile.nil? # fd.puts defaultFormName unless defaultFormName.nil? # fd.puts "Test Message to stdout!"
errorList = []
IO.readlines(defaultFormFile).each { |ln| next unless (ln =~ /^\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/) perm = $1 entity = $3 entityType = $2 ipAddr = $4 pathName = $5
# fd.puts "entity = #{entity}" if (entityType == "user" && entity == "*" && pathName !~ /^-/) then errorList << "Cannot add reference to 'user *'. Sorry." # fd.puts "(error for entity=#{entity} pathName[0]='#{pathName[0]}')" end }
if errorList.length > 0 then $stdout.puts errorList # fd.puts errorList exit(1) else exit(0) end //guest/jeff_bowles/scripts/trig_protect_paranoid.py#1 - add change 4605 (text) # Task: form trigger that refuses "p4 protect" entries that # give permissions to "user *" # # will work on 2004.2 (and later) Perforce servers, but you # need to remember to install the trigger thusly: # (p4 triggers line follows - the '#' is a comment char for Python.) # example in protect "python /path/to/scripts/on/server/machine/trig_protect_paranoid.py --formfile %formfile%"
# vi: set ts=4: # vim: set ts=4: # # num of calls to 'p4': 0 # status: tested on Darwin Mac OS X using python 2.3 # # Copyright 2004 Piccolo Engineering, Inc. All rights reserved.
import getopt import sys import re
defaultFormFile = None
[options,args] = getopt.getopt(sys.argv[1:], '', [ 'formfile=', 'ff='])
for [opt,arg] in options: if opt == "--formfile" or opt == '--ff': defaultFormFile = arg
if defaultFormFile is None: print "--formfile XXXX must be given on command-line" sys.exit(1)
errorList = []
# debugfd = open("/Users/jeffbowles/work/ruby-work/contracts/tmp-trigger-scripts/ debug.out", "w") # debugfd.writelines(["Looking at %s\n" % defaultFormFile])
fd = open(defaultFormFile, 'r') if fd is None: print "Cannot open file %s" % defaultFormFile # close(debugdb) sys.exit(1)
protect_re = re.compile('^\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)')
for ln in fd.readlines(): m = protect_re.match(ln) if m is None : continue (perm, entityType, entity, ipAddr, pathName) = m.groups() print perm, entityType, entity, ipAddr, pathName
if entityType == "user" and entity == "*" and pathName[0] != '-': errorList.append("Cannot add reference to 'user *'. Sorry.\n")
# close(fd)
if len(errorList) > 0: for e in errorList: print e # debugfd.writelines([e]) sys.exit(1) else: sys.exit(0) //guest/jeff_bowles/scripts/trig_protect_paranoid.rb#1 - add change 4603 (text) # Task: form trigger that refuses "p4 protect" entries that # give permissions to "user *" # # will work on 2004.2 (and later) Perforce servers, but you # need to remember to install the trigger thusly: # (p4 triggers line follows - the '#' is a comment char for Ruby.) # example in protect "ruby /Users/jeffbowles/work/ruby-work/contracts/tmp-trigger-scripts/ trig_protect_paranoid.p4ruby --formfile %formfile% --formname %formname%"
# # num of calls to 'p4': 0 # status: tested on Darwin Mac OS X using ruby (no P4API needed!) # # Copyright 2004 Piccolo Engineering, Inc. All rights reserved.
require "getoptlong"
options = GetoptLong.new( [ "--formfile", "--ff", GetoptLong::OPTIONAL_ARGUMENT], [ "--formname", "--fn", GetoptLong::OPTIONAL_ARGUMENT] ) defaultFormFile = nil defaultFormName = nil options.each do |opt, arg| case opt when "--formfile", "--ff" defaultFormFile = arg when "--formname", "--fn" defaultFormName = arg end end
raise Exception, "no --formfile given" if defaultFormFile.nil? raise Exception, "no --formname given" if defaultFormName.nil?
# fd = open("/Users/jeffbowles/work/ruby-work/contracts/tmp-trigger-scripts/ debug.out", "w") # fd.puts defaultFormFile unless defaultFormFile.nil? # fd.puts defaultFormName unless defaultFormName.nil? # fd.puts "Test Message to stdout!"
errorList = []
IO.readlines(defaultFormFile).each { |ln| next unless (ln =~ /^\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/) perm = $1 entity = $3 entityType = $2 ipAddr = $4 pathName = $5
# fd.puts "entity = #{entity}" if (entityType == "user" && entity == "*" && pathName !~ /^-/) then errorList << "Cannot add reference to 'user *'. Sorry." # fd.puts "(error for entity=#{entity} pathName[0]='#{pathName[0]}')" end }
if errorList.length > 0 then $stdout.puts errorList # fd.puts errorList exit(1) else exit(0) end




